//
//  RCacheCellObj.m
//  SJMediaCacheServer_Example
//
//  Created by BlueDancer on 2021/5/13.
//  Copyright © 2021 changsanjiang@gmail.com. All rights reserved.
//

#import "RCacheCellObj.h"
#import <Realm/Realm.h>
@interface RCacheCellObj ()<MCSAssetExportObserver>{
    __weak RCacheTableViewCell *_cell;
}

@end

@implementation RCacheCellObj


- (instancetype)initWithMedia:(RCacheObj *)media {
    self = [super init];
    if (self) {
        
        [SJMediaCacheServer.shared registerExportObserver:self];
        _mediaObj = media;
        
        NSURL* URL = [NSURL URLWithString:media.url];
        _exporter = [SJMediaCacheServer.shared exportAssetWithURL:URL];
        
        // 这里异步同步进度, 防止阻塞主线程
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self->_exporter synchronize];
        });
        [_exporter resume];
    }
    return self;
}

- (void)dealloc {
    [SJMediaCacheServer.shared removeExportObserver:self];
}

#pragma mark - MCSAssetExportObserver

- (void)exporter:(id<MCSAssetExporter>)exporter statusDidChange:(MCSAssetExportStatus)status {
    if (exporter == _exporter ) {
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.mediaObj.state = status;
        [[RLMRealm defaultRealm] addOrUpdateObject:self.mediaObj];
        [[RLMRealm defaultRealm] commitWriteTransaction];
    
        [self _refreshCell];
    }
}

- (void)exporter:(id<MCSAssetExporter>)exporter progressDidChange:(float)progress {
    
    if (exporter == _exporter ) {
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.mediaObj.progress = progress;
        [[RLMRealm defaultRealm] addOrUpdateObject:self.mediaObj];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        [self _refreshCell];
    }
}

#pragma mark - mark


- (void)bindCell:(RCacheTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    _cell = cell;
    [self _refreshCell];
}

- (void)unbindCell:(RCacheTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    _cell = nil;
}

#pragma mark - mark

- (void)_refreshCell {
    _cell.nameLb.text = self.mediaObj.name;
    [_cell.nameLb sizeToFit];
    
    _cell.process.progress = _exporter.progress;
    
    NSString *statusStr = nil;
    NSString *imageName = NULL;
    switch ( _exporter.status ) {
        case MCSAssetExportStatusUnknown:{
            statusStr = @"准备缓存";
            imageName = @"cache_down";
        }
            break;
        case MCSAssetExportStatusWaiting:{
            statusStr = @"等待缓存";
            imageName = @"cache_down";
        }
            break;
        case MCSAssetExportStatusExporting:{
            statusStr = @"缓存中";
            imageName = @"cache_puase";
        }
            break;
        case MCSAssetExportStatusFinished:{
            statusStr = @"已完成";
            imageName = @"cache_play";
        }
            break;
        case MCSAssetExportStatusFailed:{
            statusStr = @"缓存失败";
            imageName = @"cache_down";
        }
            break;
        case MCSAssetExportStatusSuspended:{
            statusStr = @"已暂停";
            imageName = @"cache_down";
        }
            break;
        case MCSAssetExportStatusCancelled:{
            statusStr = @"已取消";
            imageName = @"cache_down";
        }
            break;
    }
    
    [_cell.playBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [_cell.playBtn sizeToFit];
    
    _cell.stateLb.text = [NSString stringWithFormat:@"%@", statusStr];
    [_cell.stateLb sizeToFit];
    
    _cell.sizeLb.text = [NSString stringWithFormat:@"%ld%%",(long)(_exporter.progress*100)];
    [_cell.sizeLb sizeToFit];
}
@end
